/**----------------------------------------------获取地理位置--------------------------------------- */
const locationErrhandler = (vm , resolve , reject) => {
    uni.showModal({
        content: "需要授权位置信息",
        confirmText: "确认授权",
    }).then(res =>{
        // 确认授权
        if (res["confirm"]) {
            uni.openSetting({
                success:confirmRes => {
                    if (confirmRes.authSetting["scope.userLocation"]) {
                        // 授权成功
                        uni.showToast({
                            title: "授权成功",
                            icon: "none",
                        });
                        fnGetlocation(vm , location => {
                            resolve(location)
                        });
                    }else{
                        uni.showToast({
                            title: "授权失败,请重新授权",
                            icon: "none",
                        });
                        uni.showModal({
                            title: "授权",
                            content: "获取授权" + "失败,是否前往授权设置？",
                            success: result => {
                                if (result.confirm) {
                                    uni.openSetting();
                                }
                                reject()
                            },
                            fail:() =>{
                                uni.showToast({
                                    title: "系统错误！",
                                    icon: "none",
                                })
                                reject()
                            }
                        })
                    }
                }
            })
        }
        // 取消授权
        if (res["cancel"]) {
          uni.showToast({
            title: "你拒绝了授权，无法获得周边信息",
            icon: "none",
          });
          reject()
        }
    })
}

// type: 1手动触发，会指示开启定位。0不指示
export const getLocation = (type = 0 , vm) => {
    return new Promise((resolve, reject) => {
        uni.getSystemInfo({
            success(res) {
                let locationEnabled = res.locationEnabled; //判断手机定位服务是否开启
                let locationAuthorized = res.locationAuthorized; //判断定位服务是否允许微信授权
                if (locationEnabled == false || locationAuthorized == false) {
                    uni.showToast({
                        title: "请打开手机GPS",
                        icon: "none",
                    });
                    reject()
                } else {
					uni.chooseLocation({
						success: function (res) {
							console.log('位置名称：' + res.name);
							console.log('详细地址：' + res.address);
							let data = {
								name : res.name,
								address: res.address
							}
							try {
							    uni.setStorageSync('data', data);  
							} catch (e) {  
							      console.log("获取位置数据失败")
							} 
						},
						
					});
                }
            }
        })
    })
}

